//
//  ZYHCommonMethod.m
//  YiYueStore
//
//  Created by zhouzhouzhou on 2018/10/16.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "ZYHCommonMethod.h"

@implementation ZYHCommonMethod


///返回当前视图的控制器
+ (UIViewController *)getCurrentRootViewController:(UIView*)view {
    UIResponder *next = [view nextResponder];
    do {
        if ([next isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)next;
        }
        next = [next nextResponder];
    } while (next != nil);
    return nil;
}

//展示错误信息
+ (void)showErrorAlertView:(NSString*)errorStr {
    
}

+ (void)showError:(NSError*)error {
    
}

+ (void)showSuccess:(NSString*)error{
    
}

+(void)showAlertActionViewWithShowVC:(UIViewController *)vc showMsg:(NSString *)msg sureBtnBack:(STAlertBlock)complection{
    UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"温馨提醒" message:msg preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        complection(1,nil);
    }];
    UIAlertAction *actionA = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [controller addAction:action];
    [controller addAction:actionA];
    [vc presentViewController:controller animated:YES completion:nil];
}

//存取自定义数据
+(BOOL)saveAutoModelToSandBox:(id)model withPath:(NSString *)path{
    NSFileManager * fileManager =[NSFileManager defaultManager];
    NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    NSString *directryPath = [cachesPath stringByAppendingPathComponent:path];
    if(![fileManager fileExistsAtPath:directryPath]){
        [fileManager createFileAtPath:directryPath contents:nil attributes:nil];
        NSLog(@"%@",directryPath);
        BOOL isCreat = [fileManager createFileAtPath:directryPath contents:nil attributes:nil];
        if (!isCreat) {
            NSLog(@"创建路径失败");
            return NO;
        }
    }
    BOOL isSuccess = NO;
    if ([model isKindOfClass:[NSArray class]]) {
        NSArray * arrModel =[NSArray arrayWithArray:model];
        NSMutableArray * arr = [NSMutableArray arrayWithCapacity:arrModel.count];
        for (id model in arrModel) {
            if ([model isKindOfClass:[NSDictionary class]]) {
                arr = [NSMutableArray arrayWithArray:arrModel];
                break;
            }else{
                 NSDictionary * dicModel =  [model performSelector:@selector(modelToJSONObject)];
                [arr addObject:dicModel];
            }
        }
        isSuccess = [arr writeToFile:directryPath atomically:YES];
    }else if ([model isKindOfClass:[NSDictionary class]]){
        NSDictionary * dicModel =[NSDictionary dictionaryWithDictionary:model];
        isSuccess = [dicModel writeToFile:directryPath atomically:YES];
    }else{
        NSDictionary * dicModel =  [model performSelector:@selector(modelToJSONObject)];
        isSuccess = [dicModel writeToFile:directryPath atomically:YES];
    }
    return isSuccess;
    
}

+(id)getAutoModelToSandBoxWithClassName:(NSString *)className withPath:(NSString *)path{
    NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    NSString *directryPath = [cachesPath stringByAppendingPathComponent:path];
    if ([className isEqualToString:@"NSArray"]) {
        NSArray * arrData= [NSArray arrayWithContentsOfFile:directryPath];
        return arrData;
    }else{
        Class ownCollectionClass = NSClassFromString(className);
        NSDictionary * dicData = [NSDictionary dictionaryWithContentsOfFile:directryPath];
        id dicModel =  [ownCollectionClass performSelector:@selector(modelWithJSON:) withObject:dicData];
        return dicModel;
    }
//    NSLog(@"这是未知格式 需添加 getAutoModelToSandBoxWithClassName");
    return nil;
}
@end
